package dang.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dang.common.Constant;
import dang.dao.ProductDao;
import dang.entity.Book;
import dang.entity.Product;
import dang.exception.DangException;

public class BookDaoImpl  implements ProductDao {
	private Connection connection;

	public void setConnection(Connection connection) {
		System.out.println("BookDaoImpl"+connection);
		this.connection = connection;
	}


	@Override
	public List<Product> findByCatId(Integer catId, int begin, int size,String panxu) throws DangException {
		StringBuffer sql = new StringBuffer();
		List<Product> list = new ArrayList<Product>();
		// 按栏目ID查图书信息
		try {
			sql.append(" select dp.*,db.* ");
			sql.append(" from d_category_product dcp ");
			sql.append(" 	join d_product dp on(dcp.product_id=dp.id) ");
			sql.append(" 	join d_book db on(dp.id=db.id) ");
			sql.append(" where dcp.cat_id=? ");
			sql.append(" order by  "+panxu+" ");//
			sql.append(" limit ?,? ");
			PreparedStatement ps = connection.prepareStatement(
					sql.toString());
			ps.setInt(1, catId);
			//ps.setString(2, panxu);//sql.append(" order by  ? ")//这两句为何不起作用
			ps.setInt(2, begin);
			ps.setInt(3, size);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Book book = getBookInfo(rs);
				list.add(book);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
	}

	@Override
	public List<Product> findLatest(int num) throws DangException {
		StringBuffer sql = new StringBuffer();
		List<Product> list = new ArrayList<Product>();
		// 按栏目ID查图书信息
		try {
			sql.append(" select dp.*,db.* ");
			sql.append(" from d_category_product dcp ");
			sql.append(" 	join d_product dp on(dcp.product_id=dp.id) ");
			sql.append(" 	join d_book db on(dp.id=db.id) ");
			sql.append(" where dcp.cat_id=? ");
			sql.append(" order by add_time desc ");
			sql.append(" limit ? ");
			PreparedStatement ps = connection.prepareStatement(
					sql.toString());
			ps.setInt(1, Constant.TYPE_BOOK);
			ps.setInt(2, num);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Book book = getBookInfo(rs);
				list.add(book);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
	}

	private Book getBookInfo(ResultSet rs) throws SQLException {
		Book book = new Book();
		book.setAdd_time(rs.getLong("add_time"));
		book.setAuthor(rs.getString("author"));
		book.setAuthor_summary(rs.getString("author_summary"));
		book.setCatalogue(rs.getString("catalogue"));
		book.setDang_price(rs.getDouble("dang_price"));
		book.setDescription(rs.getString("description"));
		book.setFixed_price(rs.getDouble("fixed_price"));
		book.setHas_deleted(rs.getInt("has_deleted"));
		book.setId(rs.getInt("id"));
		book.setIsbn(rs.getString("isbn"));
		book.setKeywords(rs.getString("keywords"));
		book.setPrint_number(rs.getString("print_number"));
		book.setPrint_time(rs.getInt("print_time"));
		book.setProduct_name(rs.getString("product_name"));
		book.setProduct_pic(rs.getString("product_pic"));
		book.setPublish_time(rs.getLong("publish_time"));
		book.setPublishing(rs.getString("publishing"));
		book.setTotal_page(rs.getString("total_page"));
		book.setWhich_edition(rs.getString("which_edition"));
		book.setWord_number(rs.getString("word_number"));
		return book;
	}

	@Override
	public Product findById(int id)  throws DangException{
		StringBuffer sql = new StringBuffer();
			sql.append(" select dp.*,db.* ");
			sql.append(" from d_product dp  ");
			sql.append(" 	join d_book db on(dp.id=db.id) ");
			sql.append(" where dp.id=? ");
			PreparedStatement ps;
			try {
				ps = connection.prepareStatement(sql.toString());
				ps.setInt(1, id);
				ResultSet rs = ps.executeQuery();
				Book book=null;
				while(rs.next()){
					book=getBookInfo(rs);
				}
				return book;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DangException(e);
			}
			
	}

}
